<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\library\Auth;
use fast\Tree;
use think\Cache;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class Authuser extends Backend
{

    protected $relationSearch = true;
    protected $searchFields = 'id,company';

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('AuthUser');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            if ($params) {
                $params['appSecret']= $this->signKey();
                $result = $this->model->allowField(true)->save($params);
                $insertid = $this->model->getLastInsID();
                $appKey =  'SH'.($insertid+10000);//商户号
                $this->model->save(array('appKey'=>$appKey),array('id'=>$insertid));
                $this->success();
            }
            $this->error();

        }
        return $this->view->fetch();

    }

    private function signKey() {
        $code = getRandAddr();
        if ($this->addr_recode($code)) {
            return $code;
        } else {
            while(true) {
                $this->signKey();
            }
        }
    }
    /**
     * 是否重复
     */
    private function addr_recode($code) {
        if ($this->model->where(array('appSecret'=>$code))->find()) {
            return false;
        }
        return true;
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            $this->model->allowField(true)->save($params,array('id'=>$ids));
            $this->success();
        }
        $row = $this->model->get($ids);
        $this->modelValidate = true;
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    //重置秘钥
    public function reset($ids = ""){
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $appSecret= $this->signKey();
        $this->model->save(array('appSecret'=>$appSecret),array('id'=>$ids));
        $this->success();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");

        if ($ids) {
            $delIds = explode(',', $ids);
            $count = $this->model->where('id', 'in', $delIds)->delete();
            if ($count) {
                $this->success();
            }
            $this->error();
        }
        $this->error();
    }

}
